export default class Locker {
  lens: number = 0
  delayArray: Array<Function> = []
  constructor(public amount: number) {}

  lock (): Promise<void> {
    if (this.lens < this.amount) {
      this.lens++
      return new Promise(resolve => {
        resolve()
      })
    } else {
      return new Promise(resolve => {
        this.delayArray.push(resolve)
      })
    }
  }

  release (): void {
    if (this.delayArray.length) {
      this.delayArray.shift()()
    } else {
      this.lens--
    }
  }
}